package com.kuaifawu.lwnlawyerclient.Activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.kuaifawu.lwnlawyerclient.LWNNetwork.LWNNetworkCenter;
import com.kuaifawu.lwnlawyerclient.Lib.ToastView_custom;
import com.kuaifawu.lwnlawyerclient.R;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONObject;

/**
 * Created by wyk123 on 2015/6/18.
 */
public class LWNLognWebActivity extends Activity implements View.OnClickListener{
    private ImageView cancel;
    private ImageView baground;
    private ImageView bagrounded;
    private ImageView cancel_web;
    private ImageView logn;
    private String saoCode="";
    private ImageButton lognweb_back;
    private ImageView fail_bagrounded;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.logn_frament_view);
        Intent intent=getIntent();
        if(intent!=null){
            if(intent.getExtras()!=null){
                saoCode=intent.getExtras().getString("code");
            }

        }
        inItview();

    }
   private void inItview(){
    lognweb_back=(ImageButton)findViewById(R.id.lognweb_back);
    logn=(ImageView)findViewById(R.id.logn);
    cancel=(ImageView)findViewById(R.id.cancel);
    baground=(ImageView)findViewById(R.id.baground);
    bagrounded=(ImageView)findViewById(R.id.bagrounded);
    cancel_web=(ImageView)findViewById(R.id.cancel_web);
    fail_bagrounded=(ImageView)findViewById(R.id.fail_bagrounded);
    cancel.setOnClickListener(this);
    logn.setOnClickListener(this);
    cancel_web.setOnClickListener(this);
       lognweb_back.setOnClickListener(this);
       if((saoCode.equals("")||saoCode==null)&&!saoCode.equals("123")){
           fail_bagrounded.setVisibility(View.VISIBLE);
           logn.setVisibility(View.GONE);
           cancel_web.setVisibility(View.GONE);
           cancel.setVisibility(View.GONE);
           bagrounded.setVisibility(View.GONE);
           baground.setVisibility(View.GONE);
       }

       if(saoCode.equals("123")){
           baground.setVisibility(View.GONE);
           bagrounded.setVisibility(View.VISIBLE);
           logn.setVisibility(View.GONE);
           cancel.setVisibility(View.GONE);
           cancel_web.setVisibility(View.VISIBLE);
           isoutLogn=true;
          // lognweb_back.setClickable(false);
       }
   }
private boolean isoutLogn=false;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.logn:
                codeLogn(saoCode);
                break;
            case R.id.cancel:
                finish();
                break;
            case R.id.cancel_web:
                cancelWebLogn();
                break;
            case R.id.lognweb_back:
                if(isoutLogn){
                    getBackshowDialog();
                }else{
                    finish();
                }

               // finish();
                break;

        }

    }

    private void getBackshowDialog(){

       new AlertDialog.Builder(this).setTitle("您是否要退出浏览器的登录").setPositiveButton("是",new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int which) {
               cancelWebLogn();

           }
       }).setNegativeButton("否",null).show();

    }
    /**
     * 登录网页版
     * @param code
     */

    private void codeLogn(String code){
          final ProgressDialog dialog= ProgressDialog.show(this,"","登录中，请稍后。");

        LWNNetworkCenter center = LWNNetworkCenter.getInstance();
        if(!center.checkNetworkState(this)) {
            dialog.dismiss();
            ToastView_custom toast = new ToastView_custom(this);
            toast.showCustom(this, "检查网络，稍后重试");

            return;
        }
        HttpUtils http=center.getUtils();
        String string_url = center.getInstance().getLogn(code, LWNLognWebActivity.this);
        http.configCurrentHttpCacheExpiry(0);
        http.send(HttpRequest.HttpMethod.GET,string_url,new RequestCallBack<Object>() {
            @Override
            public void onSuccess(ResponseInfo<Object> objectResponseInfo) {
                dialog.dismiss();
                try{

                    JSONObject jsonObject = new JSONObject(objectResponseInfo.result.toString());
                    String string_code = jsonObject.getString("code");
                    String string_msg=jsonObject.getString("msg");
                    switch (string_code) {
                        case "10000":
                            baground.setVisibility(View.GONE);
                            bagrounded.setVisibility(View.VISIBLE);
                            logn.setVisibility(View.GONE);
                            cancel.setVisibility(View.GONE);
                            cancel_web.setVisibility(View.VISIBLE);
                            isoutLogn=true;

                            break;
                        case "10001":

                            break;
                        case "10003":
                            ToastView_custom toast = new ToastView_custom(LWNLognWebActivity.this);
                            toast.showCustom(LWNLognWebActivity.this,  string_msg);
                            Intent intent = new Intent();
                            intent.putExtra("name", "LeiPei");
                            intent.setClass(LWNLognWebActivity.this, LWNLoginActivity.class);
                            startActivity(intent);
                            LWNLognWebActivity.this.finish();
                            break;
                    }


                }catch (Exception e){
                    e.printStackTrace();

                }



            }

            @Override
            public void onFailure(HttpException e, String s) {
                dialog.dismiss();

            }


        });






    }

    /**
     * 退出网页登录
     */
    private void cancelWebLogn(){
        final ProgressDialog dialog= ProgressDialog.show(this,"","请稍后...");
        LWNNetworkCenter center = LWNNetworkCenter.getInstance();
        if(!center.checkNetworkState(this)) {
            dialog.dismiss();
            ToastView_custom toast = new ToastView_custom(this);
            toast.showCustom(this,  "检查网络，稍后重试");

            return;
        }
        HttpUtils http=center.getUtils();
        String string_url = center.getInstance().getCancelWebLogn(LWNLognWebActivity.this);
        http.configCurrentHttpCacheExpiry(0);
        http.send(HttpRequest.HttpMethod.GET,string_url,new RequestCallBack<Object>() {
            @Override
            public void onSuccess(ResponseInfo<Object> objectResponseInfo) {
                dialog.dismiss();
                try{
                    JSONObject jsonObject = new JSONObject(objectResponseInfo.result.toString());
                    String string_code = jsonObject.getString("code");
                    String string_msg=jsonObject.getString("msg");
                    switch (string_code) {
                        case "10000":
//                             Intent intent=new Intent(LWNLognWebActivity.this,MainActivity.class);
//                              startActivity(intent);

                            finish();

                            break;
                        case "10001":

                            break;
                        case "10003":

                            break;
                    }


                }catch (Exception e){
                    e.printStackTrace();
                    dialog.dismiss();
                }



            }

            @Override
            public void onFailure(HttpException e, String s) {
                dialog.dismiss();

            }


        });}
    private long firstTime=0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if(isoutLogn){
            if (System.currentTimeMillis() - firstTime > 500) {
                firstTime = System.currentTimeMillis();
              //  Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                ToastView_custom toast = new ToastView_custom(this);
                toast.showCustom(this, "再按一次退出程序");
            } else {
              Intent intent=new Intent();
                setResult(600,intent);
                finish();
            }
            return true; }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SplashScreen");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SplashScreen");
        MobclickAgent.onPause(this);
    }
}
